package com.api.main.mvc.web;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.api.main.sentinel.SentinelDemo;
import com.common.core.results.VoidResult;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.element.security.single.annotation.AuthExcludeRequest;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "sentinel")
public class SentinelController {

    private final static Logger logger = LoggerFactory.getLogger(SentinelController.class);

    private final static String USER_DETAILS = "https://oapi.dingtalk.com/topapi/v2/user/get";

    @Resource
    private SentinelDemo sentinelDemo;

    @SentinelResource(value = "demo_url")
    @GetMapping(value = "demo")
    public VoidResult demo() {
        if (null != sentinelDemo) {

            sentinelDemo.execute();
        }
        return VoidResult.of();
    }

    @AuthExcludeRequest
    @GetMapping(value = "demo1")
    public VoidResult demo1() {
        return VoidResult.of();
    }

    public static void main(String[] args) {
//        String accessToken = "7ca5941bfbbc340ba99e738e6eb48513";
//        execute(accessToken);
        List<String> userIds = new ArrayList<>();
        userIds.add("1111");
        userIds.add("1111");
        userIds.add("1111");
        System.out.println(userIds.contains(null));
    }

    private static void execute(String accessToken) {
        try {
            DingTalkClient client = new DefaultDingTalkClient(USER_DETAILS);
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid("2319050549873205");
            req.setLanguage("en_US");
            OapiV2UserGetResponse rsp = client.execute(req, accessToken);
            logger.info("结果:{}", rsp.getBody());
        } catch (ApiException e) {
            logger.error("钉钉接口异常:", e);
        }
    }
}